<?php
namespace Tongcheng\Controller;
use Think\Controller;
class InfoCateController extends CommonController {
    public function index(){
    	$model = M("Cate");
    	$map['wid'] = session('wid');
        $map['type'] = 'index';
    	$list = $model->where($map)->select();
        unset($map['type']);
        foreach ($list as $key => $value) {
            $map['pid'] = $value['id'];
            $list[$key]['sub_cate'] = $model->where($map)->select();
        }
        $this->assign('list',$list);
        $this->display();
    }

    public function add(){
    	if(IS_AJAX){
            $model = M("Cate");
            $new['wid'] = session('wid');
            $new['pid'] = 0;
            $new['type'] = 'index';
            $new['sub_type'] = I('post.sub_type');
            if($new['sub_type'] == 'common'){
                $new['need_id'] = 1;
            }
            if($new['sub_type'] == 'coupon'){
                $new['can_add'] = 0;
            }
            $new['title'] = I('post.title');
            $new['url'] = I('post.sub_type') == 'coupon'?"/pages/shop/coupon/coupon":I('post.sub_type')."/index";
            $new['tags'] = str_replace('，',',',I('post.tags'));
            $new['imgurl'] = I('post.imgurl');
            if($pid = $model->add($new)){
                $data['imgurl'] = "";
                if($new['sub_type'] == 'job'){
                    $data['title'] = "全职招聘";
                    $data['wid'] = session('wid');
                    $data['pid'] = $pid;
                    $data['sub_type'] = "quanzhi";
                    $data['url'] = "";
                    $arr[] = $data;

                    $data['title'] = "求职简历";
                    $data['wid'] = session('wid');
                    $data['pid'] = $pid;
                    $data['sub_type'] = "qiu";
                    $data['url'] = "";
                    $arr[] = $data;

                    $data['title'] = "兼职招聘";
                    $data['wid'] = session('wid');
                    $data['pid'] = $pid;
                    $data['sub_type'] = "jianzhi";
                    $data['url'] = "";
                    $arr[] = $data;
                    $model->addAll($arr);
                }elseif($new['sub_type'] == 'house'){
                    $data['title'] = "新房";
                    $data['wid'] = session('wid');
                    $data['pid'] = $pid;
                    $data['sub_type'] = "new";
                    $data['url'] = "";
                    $arr[] = $data;

                    $data['title'] = "二手房";
                    $data['wid'] = session('wid');
                    $data['pid'] = $pid;
                    $data['sub_type'] = "ershou";
                    $data['url'] = "";
                    $arr[] = $data;

                    $data['title'] = "租房";
                    $data['wid'] = session('wid');
                    $data['pid'] = $pid;
                    $data['sub_type'] = "zufang";
                    $data['url'] = "";
                    $arr[] = $data;
                    $model->addAll($arr);
                }elseif($new['sub_type'] == 'pinche'){
                    $data['title'] = "人找车";
                    $data['wid'] = session('wid');
                    $data['pid'] = $pid;
                    $data['sub_type'] = "zhaoche";
                    $data['url'] = "";
                    $arr[] = $data;

                    $data['title'] = "车找人";
                    $data['wid'] = session('wid');
                    $data['pid'] = $pid;
                    $data['sub_type'] = "zhaoren";
                    $data['url'] = "";
                    $arr[] = $data;
                    $model->addAll($arr);
                }
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $arr[] = array('title'=>'通用模块', 'value'=>'common');
            $arr[] = array('title'=>'招聘模块', 'value'=>'job');
            $arr[] = array('title'=>'房产模块', 'value'=>'house');
            $arr[] = array('title'=>'拼车模块', 'value'=>'pinche');
            $arr[] = array('title'=>'优惠券模块', 'value'=>'coupon');
            $this->assign('arr',$arr);
            $this->display();
        }
    }

    public function edit(){
        $model = M("Cate");
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
    	if(IS_AJAX){
            $new['sub_type'] = I('post.sub_type');
            if($new['sub_type'] == 'common'){
                $new['need_id'] = 1;
            }
            if($new['sub_type'] == 'coupon'){
                $new['can_add'] = 0;
            }
            $new['title'] = I('post.title');
            $new['url'] = I('post.sub_type') == 'coupon'?"/pages/shop/coupon/coupon":I('post.sub_type')."/index";
            $new['tags'] = str_replace('，',',',I('post.tags'));
            $new['imgurl'] = I('post.imgurl');
            $model->where($map)->save($new);

            $eq['wid'] = session('wid');
            $pid = $map['id'];
            $eq['pid'] = $pid;
            $data['imgurl'] = "";
            if($new['sub_type'] == 'job'){
                $model->where($eq)->delete();
                $data['title'] = "全职招聘";
                $data['wid'] = session('wid');
                $data['pid'] = $pid;
                $data['sub_type'] = "quanzhi";
                $data['url'] = "";
                $arr[] = $data;

                $data['title'] = "求职简历";
                $data['wid'] = session('wid');
                $data['pid'] = $pid;
                $data['sub_type'] = "qiu";
                $data['url'] = "";
                $arr[] = $data;

                $data['title'] = "兼职招聘";
                $data['wid'] = session('wid');
                $data['pid'] = $pid;
                $data['sub_type'] = "jianzhi";
                $data['url'] = "";
                $arr[] = $data;
                $model->addAll($arr);
            }elseif($new['sub_type'] == 'house'){
                $model->where($eq)->delete();
                $data['title'] = "新房";
                $data['wid'] = session('wid');
                $data['pid'] = $pid;
                $data['sub_type'] = "new";
                $data['url'] = "";
                $arr[] = $data;

                $data['title'] = "二手房";
                $data['wid'] = session('wid');
                $data['pid'] = $pid;
                $data['sub_type'] = "ershou";
                $data['url'] = "";
                $arr[] = $data;

                $data['title'] = "租房";
                $data['wid'] = session('wid');
                $data['pid'] = $pid;
                $data['sub_type'] = "zufang";
                $data['url'] = "";
                $arr[] = $data;
                $model->addAll($arr);
            }elseif($new['sub_type'] == 'pinche'){
                $model->where($eq)->delete();
                $data['title'] = "人找车";
                $data['wid'] = session('wid');
                $data['pid'] = $pid;
                $data['sub_type'] = "zhaoche";
                $data['url'] = "";
                $arr[] = $data;

                $data['title'] = "车找人";
                $data['wid'] = session('wid');
                $data['pid'] = $pid;
                $data['sub_type'] = "zhaoren";
                $data['url'] = "";
                $arr[] = $data;
                $model->addAll($arr);
            }
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);

            $arr[] = array('title'=>'通用模块', 'value'=>'common');
            $arr[] = array('title'=>'招聘模块', 'value'=>'job');
            $arr[] = array('title'=>'房产模块', 'value'=>'house');
            $arr[] = array('title'=>'拼车模块', 'value'=>'pinche');
            $arr[] = array('title'=>'优惠券模块', 'value'=>'coupon');
            $this->assign('arr',$arr);
            $this->display('add');
        }
    }

    public function sub_add(){
        $model = M("Cate");

        if(IS_AJAX){
            $new['wid'] = session('wid');
            $new['pid'] = $_GET['id'];
            $new['sub_type'] = I('post.sub_type');
            if($new['sub_type'] == 'common'){
                $new['need_id'] = 1;
            }
            $new['title'] = I('post.title');
            $new['url'] = I('post.url');
            $new['tags'] = str_replace('，',',',I('post.tags'));
            $new['imgurl'] = I('post.imgurl');
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $new['id'] = $_GET['id'];
            $pcate = $model->where($new)->find();
            switch ($pcate['sub_type']) {
                case 'common':
                    $arr[] = array('title'=>'通用模块', 'value'=>'common');
                    break;
                default:
                    $this->error('该分类不支持添加子分类');
                    break;
            }
            $this->assign('arr',$arr);
            $this->display('add');
        }
    }

    public function sub_edit(){
        $model = M("Cate");
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['sub_type'] = I('post.sub_type');
            if($new['sub_type'] == 'common'){
                $new['need_id'] = 1;
            }
            $new['title'] = I('post.title');
            $new['url'] = I('post.url');
            $new['tags'] = str_replace('，',',',I('post.tags'));
            $new['imgurl'] = I('post.imgurl');
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);

            $new['id'] = $vo['pid'];
            $pcate = $model->where($new)->find();
            switch ($pcate['sub_type']) {
                case 'house':
                    $arr[] = array('title'=>'新房模块', 'value'=>'new');
                    $arr[] = array('title'=>'二手房模块', 'value'=>'ershou');
                    $arr[] = array('title'=>'租房模块', 'value'=>'zufang');
                    break;
                case 'common':
                    $arr[] = array('title'=>'通用模块', 'value'=>'common');
                    break;
                case 'job':
                    $arr[] = array('title'=>'全职招聘', 'value'=>'quanzhi');
                    $arr[] = array('title'=>'求职简历', 'value'=>'qiu');
                    $arr[] = array('title'=>'兼职招聘', 'value'=>'jianzhi');
                    break;
                case 'pinche':
                    $arr[] = array('title'=>'人找车', 'value'=>'zhaoren');
                    $arr[] = array('title'=>'车找人', 'value'=>'zhaoche');
                    break;
                default:
                    $this->error('该分类不支持添加子分类');
                    break;
            }
            $this->assign('arr',$arr);
            $this->display('add');
        }
    }

    public function del(){
        $model = M("Cate");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
}